package achievement.dao.Impl;

import achievement.dao.SzlPatentDao;
import achievement.domain.SzlPatent;
import cn.hutool.core.util.StrUtil;
import utils.JDBCUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class SzlPatentDaoImpl implements SzlPatentDao {
    private Connection con;
    private PreparedStatement psmt;
    private ResultSet rs;
    @Override
    public void insert(SzlPatent szlPatent) {
        String sql = "insert into szl_patent\n" +
                "(patent_name,patent_type,patent_no,patent_time,patent_status,patent_rank,patent_proof) \n" +
                "values(?,?,?,?,?,?,?)";
        try {
            con = JDBCUtils.getConnection();
            psmt = con.prepareStatement(sql);
            psmt.setString(1,szlPatent.getPatentName());
            psmt.setString(2,szlPatent.getPatentType());
            psmt.setString(3,szlPatent.getPatentNo());
            psmt.setObject(4,szlPatent.getPatentTime());
            psmt.setString(5,szlPatent.getPatentStatus());
            psmt.setInt(6,szlPatent.getPatentRank());
            psmt.setString(7,szlPatent.getPatentProof());
            psmt.executeUpdate();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.closeResource(con,psmt);
        }
    }

    @Override
    public void updateById(SzlPatent szlPatent) {
        String sql = "update szl_patent set";
        List<Object> param = new ArrayList<>();
        if (szlPatent == null){
            return;
        }else{
            if(szlPatent.getPatentId() != null){
                sql += " patent_id = ?,";
                param.add(szlPatent.getPatentId());
            }
            if(szlPatent.getPatentName() != null){
                sql += " patent_name = ?,";
                param.add(szlPatent.getPatentName());
            }
            if(szlPatent.getType() != null){
                sql += " type = ?,";
                param.add(szlPatent.getType());
            }
            if(szlPatent.getPatentType() != null){
                sql += " patent_type = ?,";
                param.add(szlPatent.getPatentType());
            }
            if(szlPatent.getPatentNo() != null){
                sql += " patent_no = ?,";
                param.add(szlPatent.getPatentNo());
            }
            if(szlPatent.getPatentTime() != null){
                sql += " patent_time = ?,";
                param.add(szlPatent.getPatentTime());
            }
            if(szlPatent.getPatentStatus() != null){
                sql += " patent_status = ?,";
                param.add(szlPatent.getPatentStatus());
            }
            if(szlPatent.getPatentRank() != null){
                sql += " patent_rank = ?,";
                param.add(szlPatent.getPatentRank());
            }
            if(szlPatent.getPatentProof() != null){
                sql += " patent_proof = ?,";
                param.add(szlPatent.getPatentProof());
            }
        }
        sql = StrUtil.strip(sql,null,",");
        sql+=" where patent_id = ?";
        try {
            con = JDBCUtils.getConnection();
            psmt = con.prepareStatement(sql);
            for(int i = 0; i < param.size(); i++){
                psmt.setObject(i+1,param.get(i));
            }
            psmt.setLong(param.size()+1,szlPatent.getPatentId());
            psmt.executeUpdate();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.closeResource(con,psmt);
        }
    }

    @Override
    public void deleteById(Long id) {
        String sql = "delete from szl_patent where patent_id = ?";
        try {
            con = JDBCUtils.getConnection();
            psmt = con.prepareStatement(sql);
            psmt.setLong(1,id);
            psmt.executeUpdate();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.closeResource(con,psmt);
        }
    }

    @Override
    public SzlPatent selectById(Long id) {
        String sql = "select * from szl_patent where patent_id = ?";
        SzlPatent szlPatent = new SzlPatent();
        try {
            con = JDBCUtils.getConnection();
            psmt = con.prepareStatement(sql);
            psmt.setLong(1,id);
            rs = psmt.executeQuery();
            while (rs.next()){
                szlPatent.setPatentId(rs.getLong(1));
                szlPatent.setPatentName(rs.getString(2));
                szlPatent.setType(rs.getInt(3));
                szlPatent.setPatentType(rs.getString(4));
                szlPatent.setPatentNo(rs.getString(5));
                szlPatent.setPatentTime(rs.getDate(6));
                szlPatent.setPatentStatus(rs.getString(7));
                szlPatent.setPatentRank(rs.getInt(8));
                szlPatent.setPatentProof(rs.getString(9));
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.closeResource(con,psmt,rs);
        }
        return szlPatent;
    }

    @Override
    public List<SzlPatent> selectList(SzlPatent szlPatent) {
        String sql = "select * from szl_patent where 1=1";
        List<SzlPatent> list = new ArrayList<>();
        List<Object> param = new ArrayList<>();
        if (szlPatent == null){
            sql = "select * from szl_patent";
        }else{
            if(szlPatent.getPatentId() != null){
                sql += " and patent_id = ?";
                param.add(szlPatent.getPatentId());
            }
            if(szlPatent.getPatentName() != null){
                sql += " and patent_name like \"%\"?\"%\"";
                param.add(szlPatent.getPatentName());
            }
            if(szlPatent.getType() != null){
                sql += " and type = ?";
                param.add(szlPatent.getType());
            }
            if(szlPatent.getPatentType() != null){
                sql += " and patent_type like \"%\"?\"%\"";
                param.add(szlPatent.getPatentType());
            }
            if(szlPatent.getPatentNo() != null){
                sql += " and patent_no like \"%\"?\"%\"";
                param.add(szlPatent.getPatentNo());
            }
            if(szlPatent.getPatentTime() != null){
                sql += " and patent_time = ?";
                param.add(szlPatent.getPatentTime());
            }
            if(szlPatent.getPatentStatus() != null){
                sql += " and patent_status like \"%\"?\"%\"";
                param.add(szlPatent.getPatentStatus());
            }
            if(szlPatent.getPatentRank() != null){
                sql += " and patent_rank = ?";
                param.add(szlPatent.getPatentRank());
            }
            if(szlPatent.getPatentProof() != null){
                sql += " and patent_proof like \"%\"?\"%\"";
                param.add(szlPatent.getPatentProof());
            }
        }
        try {
            con = JDBCUtils.getConnection();
            psmt = con.prepareStatement(sql);
            for(int i = 0; i < param.size(); i++){
                psmt.setObject(i+1,param.get(i));
            }
            rs = psmt.executeQuery();
            while (rs.next()){
                SzlPatent szlPatent1 = new SzlPatent();
                szlPatent1.setPatentId(rs.getLong(1));
                szlPatent1.setPatentName(rs.getString(2));
                szlPatent1.setType(rs.getInt(3));
                szlPatent1.setPatentType(rs.getString(4));
                szlPatent1.setPatentNo(rs.getString(5));
                szlPatent1.setPatentTime(rs.getDate(6));
                szlPatent1.setPatentStatus(rs.getString(7));
                szlPatent1.setPatentRank(rs.getInt(8));
                szlPatent1.setPatentProof(rs.getString(9));
                list.add(szlPatent1);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            JDBCUtils.closeResource(con,psmt,rs);
        }
        return list ;
    }
}
